%%%-------------------------------------------------------------------
%%% File    : hiberl_utils.erl
%%% Author  : alfonso <alfonso@alfonso-laptop>
%%% Description : 
%%%
%%% Created : 18 Sep 2009 by alfonso <alfonso@alfonso-laptop>
%%%-------------------------------------------------------------------
-module(hiberl_utils).

-compile(export_all).

happy_keysearch(Key,L)->
    {value,{_,Value}} = lists:keysearch(Key, 1, L),
    Value.

low_first(Table) when is_list(Table)->
    [ string:to_lower(hd(Table)) | tl(Table) ];
low_first(X) ->X.

cap_first(Table) when is_list(Table)->
    [ string:to_upper(hd(Table)) | tl(Table) ];
cap_first(X) ->X.

pp(L)->
    pp(L,10).

pp([],N)->
    ok;
pp([H|_] = L,N)->
    Table = element(1,H),
    {L2,L3} = lists:splitwith(fun(X)->element(1,H) == element(1,X) end, L),
    io:format("Table: ~s~n",[ Table ]),
    lists:map(fun pp2/1, [Table:columns()]),    
    io:format("~s~n",[ ["-"|| X <-lists:seq(1,125) ]  ]),
    lists:map(fun pp2/1, lists:sublist([Table:get_data(Row) || Row<-L2],N)),
    io:format("~n",[]),
    pp(L3,N).

pp2([ X | R ]) when is_list(X)->
    io:format("~s",[string:left(X,15,$ )]),
    pp2(R);
pp2([ X | R ]) ->
    io:format("~s ", [string:left(io_lib:format("~p",[X]),15,$ )]),
    pp2(R);
pp2([]) ->
    io:format("~n",[]).


sql_print(X) when is_list(X)->
    S = lists:reverse(tl(lists:reverse(tl(io_lib:print(X))))),
    ScapeS = re:replace(S,"'","\\\\'",[{return,list}, global]),
    [$', ScapeS, $'];

sql_print( X ) ->
    io_lib:print(X).

